package BasicEntity
{
	import flash.geom.Vector3D;
	public class SteeringBehaviors
	{
		public var VehicleIn:Vehicle;
		
		public var t:int = 0;
		
		public var d:Boolean = true;
		
		public function SteeringBehaviors(_vehicle:Vehicle)
		{
			VehicleIn = _vehicle;
		}
		
		/**
		 * 计算Vehicle的受力情况,返回一个合力
		 */
		public function Calculate():Vector2D
		{
			EvadeObstacle();
			trace("");
			switch(t)
			{
				case 65:  //A
					return Seek(new Vector2D(400, 300));
					break;
				case 83:  //S
					return Flee(new Vector2D(400, 300));
					break;
				case 68:  //D
					return Arrive(new Vector2D(400, 300),50);
					break;
				case 70:  //F
					return VehicleIn.radar.Wander();
					break;
				case 71://G
					if((VehicleIn as Hunter) != null)
					    return Pursuit(VehicleIn as Hunter);
					return VehicleIn.radar.Wander();
					break;
				case 72://H
					if (d)
					{
					  d = !d;
					  return TurnAngle(90);
					}
					else
					{
						return new Vector2D(0, 0);
					}
					
					
				default:
					return new Vector2D();
			}		
			
		}
		
		/**
		 * 返回一个操控实体到达目标位置的力
		 */
		public function Seek(_targetPos:Vector2D):Vector2D
		{
			var desiredVelocity:Vector2D = ((_targetPos.Subtract(VehicleIn.Pos)).Normalize()).CrossProduct(VehicleIn.MaxSpeed);
			var result:Vector2D = desiredVelocity.Subtract(VehicleIn.Velocity);
			return result;		    
		}
		
		public function Flee(_targetPos:Vector2D):Vector2D
		{
			var desireVelocity:Vector2D = (VehicleIn.Pos.Subtract(_targetPos)).CrossProduct(0.001);
			var result:Vector2D = desireVelocity.Subtract(VehicleIn.Velocity);
			return result;
		}
		
		public function Arrive(_targetPos:Vector2D, _deceleration:Number):Vector2D
		{
			var toTarget:Vector2D = _targetPos.Subtract(VehicleIn.Pos);
			var dist:Number = toTarget.Length;
			if (dist > 0)
			{
				var speed:Number = dist / _deceleration * 0.3;
				speed = Math.min(speed, VehicleIn.MaxSpeed);
				
				var desiredVelocity:Vector2D = (toTarget.CrossProduct(speed)).CrossProduct(1.0 / dist);
				return(desiredVelocity.Subtract(VehicleIn.Velocity));
			}
			else
			{
				return new Vector2D(0, 0);
			}			
		}
		
		public function Pursuit(_hunter:Hunter):Vector2D
		{
			var angleOffset:Number = _hunter.quarry.Pos.GetAngleOffset(_hunter.Pos);
			if (angleOffset < -0.95)
			{
				return Seek(_hunter.quarry.Pos);
			}
			else
			{
				var lookHeadTime:Number = _hunter.quarry.Pos.Length / (_hunter.MaxSpeed + _hunter.quarry.Velocity.Length);
				var target:Vector2D = _hunter.quarry.Pos.Addition(_hunter.quarry.Velocity.CrossProduct(lookHeadTime));
				return Seek(target);
			}
		}
		
		/**
		 * 以一个角度（角度为单位）转向
		 * _angle:角度 
		 */
		public function TurnAngle(_angle:int):Vector2D
		{
			var angle:Number = _angle * Math.PI / 180;
			var pAngle:Number = VehicleIn.Velocity.Angle;
			var da:Number = pAngle - angle;
			var v:Vector2D = new Vector2D(100, 100);
			v.Angle = da;
			v.Length =  VehicleIn.Velocity.Length;
			var re:Vector2D = v.Subtract(VehicleIn.Velocity)
				
			
			var desiredVelocity:Vector2D = (re.Normalize()).CrossProduct(VehicleIn.MaxSpeed);
			var result:Vector2D = desiredVelocity.Subtract(VehicleIn.Velocity);
			return result;		    
		}
		
		public function EvadeObstacle():Vector2D
		{
			for each(var item:Obstacle in GameWorld.Obstacles)
			{
				var lcoordinator:Vector2D = GameWorld.WorldToLocal(item.Pos, VehicleIn.Pos, VehicleIn.Heading);
			    trace("ID:", item.ID, ":", int(lcoordinator.Angle * 180 / Math.PI));
				
				
			}
			
			return new Vector2D();
		}
		

		
	}
}